package com.flower.subject.domain.handler.subject;

import com.flower.subject.domain.convent.MultipleSubjectConverter;
import com.flower.subject.domain.convent.RadioSubjectConverter;
import com.flower.subject.domain.entity.SubjectAnswerBO;
import com.flower.subject.domain.entity.SubjectInfoBO;
import com.flower.subject.domain.entity.SubjectOptionBO;
import com.flower.subject.enums.SubjectInfoTypeEnum;
import com.flower.subject.infra.basic.entity.SubjectMultiple;
import com.flower.subject.infra.basic.entity.SubjectRadio;
import com.flower.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-08 14:48
 */

@Component
public class RadioTypeHandler implements SubjectTypeHandler{


    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {

        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        if(subjectInfoBO != null){
            subjectInfoBO.getOptionList().forEach(option -> {
                SubjectRadio radio = RadioSubjectConverter.INSTANT.convertBoToEntity(option);
                radio.setSubjectId(subjectInfoBO.getId().intValue());

                subjectRadioList.add(radio);
            });

            subjectRadioService.batchInsert(subjectRadioList);
        }







    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        List<SubjectRadio> radios = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> radioList = RadioSubjectConverter.INSTANT.covertAnswerBoToBOList(radios);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(radioList);
        return subjectOptionBO;
    }
}
